﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Imageo.bintje;
using log4net;
using System.Threading;

namespace Imageo.ui
{
    public partial class Home : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int proUserId = 0;
                string strProUserId = Request.QueryString["prouserid"] as string;

                if (!string.IsNullOrEmpty(strProUserId))
                {
                    if (!int.TryParse(strProUserId, out proUserId))
                    {
                        Response.Redirect("~/UI/ProErrorGeneric.aspx");
                    }
                    // We have a professional user here
                    divIPhone.Visible = false;
                    divImageDescription.Visible = false;
                    divImageoLearn.Visible = false;
                    pnlSearchArea.Visible = false;
                }
                else
                {
                    // This is not a pro user
                }

                bintje.imageoSession currentSession = Session["BintjeSession"] as imageoSession;
                ServiceAPI service = new ServiceAPI();

                IList<trailWrapper> trailList;
                IList<trailWrapper> latestList;
                IList<trailWrapper> popularList;

                trailListResponse trailsResponse;
                trailListResponse latestResponse;
                trailListResponse popularResponse;

                searchRequest latestRequest = new searchRequest();
                latestRequest.searchType = "all";
                latestRequest.numberOfTrails = 3;
                latestRequest.firstCount = 0;
                latestRequest.sortingField = "created";
                latestRequest.descending = true;
                latestRequest.session = currentSession;
                trailTableLatest.ShowMoreUrl = ResolveUrl(UrlHelper.getUrl("~/UI/TrailSearch2.aspx", proUserId, new string[] { "sortby=created" }));
                if (proUserId > 0)
                {
                    latestRequest.searchWithinUser = true;
                    latestRequest.userid = proUserId;
                    latestRequest.professionalUser = proUserId;
                }
                latestResponse = ServiceHelper.HandleBintjeErrors(this, service.getTrails(latestRequest));


                searchRequest popularRequest = new searchRequest();
                popularRequest.searchType = "all";
                popularRequest.numberOfTrails = 3;
                popularRequest.firstCount = 0;
                popularRequest.sortingField = "visited";
                popularRequest.descending = true;
                popularRequest.session = currentSession;
                trailTablePopular.ShowMoreUrl = ResolveUrl(UrlHelper.getUrl("~/UI/TrailSearch2.aspx", proUserId, new string[] { "sortby=visited" }));
                if (proUserId > 0)
                {
                    trailTablePopular.ShowMoreUrl = ResolveUrl("~/UI/TrailSearch2.aspx?sortby=visited") + "&prouserid=" + proUserId.ToString();
                    popularRequest.searchWithinUser = true;
                    popularRequest.userid = proUserId;
                    popularRequest.professionalUser = proUserId;
                }
                popularResponse = ServiceHelper.HandleBintjeErrors(this, service.getTrails(popularRequest));

                searchRequest trailsRequest = new searchRequest();
                trailsRequest.searchType = "all";
                trailsRequest.numberOfTrails = 50;
                trailsRequest.firstCount = 1;
                trailsRequest.sortingField = "created";
                trailsRequest.descending = true;
                trailsRequest.session = currentSession;
                if (proUserId > 0)
                {
                    trailsRequest.searchWithinUser = true;
                    trailsRequest.userid = proUserId;
                    trailsRequest.professionalUser = proUserId;
                }
                trailsResponse = ServiceHelper.HandleBintjeErrors(this, service.getTrails(trailsRequest));

                // Now load the trails in the map
                trailList = trailsResponse.trailList;
                gmapTrails.LoadTrails(trailList);

                latestList = new List<trailWrapper>();
                popularList = new List<trailWrapper>();

                trailTableLatest.LoadTrails(latestResponse.trailList, proUserId);
                trailTablePopular.LoadTrails(popularResponse.trailList, proUserId);
            }
            catch (ThreadAbortException) {}
            catch(Exception exc)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error(exc.Source+" - "+ exc.Message);
                }
                Response.Redirect("~/UI/ErrorGeneric.aspx");
            }
        }
    }
}
